package cn.phecda.boot.common.lang.model;

import cn.phecda.boot.common.lang.constants.DefaultConsts;
import com.alibaba.ttl.TransmittableThreadLocal;
import org.apache.commons.lang3.ObjectUtils;

/**
 * <p> 多租户上下文 </p>
 *
 * @author : wanpinwei
 */
public class TenantContext {

    private static final ThreadLocal<String> CURRENT_TENANT_ID = new TransmittableThreadLocal<>();

    public static String get() {
        String code = CURRENT_TENANT_ID.get();
        if (ObjectUtils.isEmpty(code)) {
            return DefaultConsts.TENTANT_CODE;
        }
        return code;
    }

    public static void set(String tenantId) {
        CURRENT_TENANT_ID.set(tenantId);
    }

    public static void clear() {
        CURRENT_TENANT_ID.remove();
    }

}
